/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.interceptor;

//import com.cashmate.controller.PrivilegeDAO;
//import com.cashmate.model.*;
//import com.cashmate.view.*;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.tces.model.Student;
import com.tces.model.User;
import com.tces.view.AuthenticationAction;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author CMA Developer
 */
public class GlobalInterceptor extends AbstractInterceptor {

    private static final Logger logger = Logger.getLogger(GlobalInterceptor.class);

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        logger.info("================================= START " + actionInvocation.getProxy().getActionName() + " =================================");
        logger.info("Package Name :" + actionInvocation.getProxy().getNamespace());
        String result="";//"view-employee-login";
        Map<String, Object> session = actionInvocation.getInvocationContext().getSession();
        Object action = actionInvocation.getAction();
        if (action instanceof AuthenticationAction) {
            logger.info("This Action is excempted from authentication");
            result = actionInvocation.invoke();
        } else {
            if (session.containsKey("user")) {
                User user = (User) session.get("user");
                    if (user != null) {
                        System.out.println("EMPLOYEE SESSION is NOT NULL");
                        logger.info("EMPLOYEE SESSION : " + user.getUserLastName() + ", " + user.getUserFirstName() + " " + user.getUserMiddleName());
//                        if (!(actionInvocation.getProxy().getActionName().equals("view") && actionInvocation.getProxy().getNamespace().equalsIgnoreCase("/branch")) && !(actionInvocation.getProxy().getActionName().equals("logout"))) {
//                            System.out.println("THIS is not VIEWING Branch");
//                            logger.info("Passed Authentication Interceptor.");
//                            if (branch != null && branch.getWebStatus() > 0) {
//                                logger.info(branch.getBranchName() + " is Online - " + branch.getWebStatus());
//                                String actionName=actionInvocation.getProxy().getActionName();
//                                if(actionName.contains("~")){
//                                    actionName=actionName.substring(0, actionName.indexOf("~"));
//                                }
//                                if(PrivilegeDAO.checkPermission(student.getEmployeeId(), actionName, actionInvocation.getProxy().getNamespace().substring(1))){
//                                    result = actionInvocation.invoke();
//                                }else{
//                                    result="error";
//                                logger.info("No Permission!");
//                                }
//                                
//                            } else {
//                                logger.info("Branch Session is null");
//                                result = "view-branch-login";
//                            }
//                        } else {
//                            result = actionInvocation.invoke();
//                        }
                        result = actionInvocation.invoke();
                    } else {
                        logger.info("EMPLOYEE SESSION : " + user);
                        result = "view~student-authentication";
                    }
            } else {
                logger.info("ACTION NAME NULL SESSION:" + actionInvocation.getAction());
                if (action instanceof ValidationAware) {
                    ((ValidationAware) action).addActionError("Unauthorized access. Please Login first");
                }
                result = "view~student-authentication";
                logger.info("Unauthorized access denied!");
                logger.info("return result : " + result);
                logger.info("================================= END " + actionInvocation.getProxy().getActionName() + " =================================");
                return "view~student-authentication";
            }
        }
        logger.info("INTERCEPT result : " + result);
        logger.info("================================= END " + actionInvocation.getProxy().getActionName() + " =================================");
        return result;
    }
}
